use std::cmp::Ordering;
use crate::solution::Solution;

impl Solution {
    pub fn hardest_worker(_n: i32, logs: Vec<Vec<i32>>) -> i32 {
        // 看不懂！！
        // 转移所有权：vec.into_iter() // 好像是没有i循环的？？
        logs.into_iter().fold((0, 0, 0), |(id, time, pre), log| {
            match (log[1] - pre).cmp(&time) {
                Ordering::Greater => (log[0], log[1] - pre, log[1]),
                Ordering::Equal if log[0] < id =>  (log[0], time, log[1]),
                Ordering::Equal | Ordering::Less =>  (id, time, log[1]),
            }
        }).0

    }

    pub fn hardest_worker_two(n: i32, logs: Vec<Vec<i32>>) -> i32 {
        let (mut id, mut time) = (n, -1);
        logs.into_iter().fold(0, |prev, log| {
            match time.cmp(&(log[1] - prev)) {
                // Ordering::Less => (id, time) = (log[0], log[1] - prev),
                Ordering::Less => {
                    id = log[0];
                    time = log[1] - prev;
                }
                Ordering::Equal => id = id.min(log[0]),
                Ordering::Greater => (),
            }
            log[1]
        });
        id
    }

}